Ext.namespace('GBIF');
GBIF.AssociationGrid = function(config){
	this.pageSize = 100;
	var store = new Ext.data.JsonStore({
			url: '../../resources/api/savedSearch.php'
		,	root: 'records'
		,	baseParams: { 
					cmd: 'listAssociation'
				,	limit: this.pageSize
			}
		,	totalProperty: 'recordCount'
		,	fields: [	
					{name: 'association', mapping: 'association'}
			]			
		,	remoteSort: true
		,	sortInfo: { field: "association", direction: "ASC" }
		,	listeners: {
				loadexception: function() {
					GBIF.Notice.msg('Error','Error in connection.');
				}
			}
	});
	var columns = [
			{header: 'Association', width: 60, sortable: true, dataIndex: 'association'}
	]
	Ext.apply(this, config, {
			store: store
		,	columns: columns
		,	sm: new Ext.grid.RowSelectionModel({singleSelect:true})
		,	autoScroll: true
		, 	loadMask: true
		,	border: false
		,	viewConfig: {
					forceFit: true
				,	deferEmptyText: false	
				,	emptyText: 'No Association Available.'
			}
		,	tbar: [{
						text: 'Delete Association'
					,	scope: this	
					,	handler: function() {
							var selected = this.getSelectionModel().getSelected();
							if(this.getStore().getCount() != 0){
								if(Ext.isDefined(selected)){
									this.deleteAssociation(GBIF.currentTerm, selected.data.association);
								} else {
									GBIF.Notice.msg('Notice','Please select record.');
								}
							}else {
								GBIF.Notice.msg('Notice','No association available.');
							}
						}  
			}]
		,	listeners: {
					rowcontextmenu: this.showContextMenu
			}	
	});
	GBIF.AssociationGrid.superclass.constructor.call(this, config);
}

Ext.extend(GBIF.AssociationGrid, Ext.grid.GridPanel, {
		showContextMenu: function(grid, row, e){
				grid.getSelectionModel().selectRow(row);
				var record = grid.getSelectionModel().getSelected().data;
				var items = [];
				
				items.push({
						text: 'Delete Association'
					,	scope: this	
					,	handler: function() {
							this.deleteAssociation(GBIF.currentTerm, record.association);
						}  
				});
				
				var menu = new Ext.menu.Menu({
					items: items
				});
				
				var xy = e.getXY();
				menu.showAt(xy);
		}
	,	deleteAssociation: function(term, association){
			var str = String.format('Are you sure delete {0} ?', association);
			Ext.Msg.confirm('Notice', str, function(btn){
				if(btn == 'yes'){
					Ext.Ajax.request({
							url: '../../resources/api/savedSearch.php'
						,	params: { 
								cmd: 'deleteAssociation'			
							,	term: term
							,	association: association
							}
						,	scope: this	
						,	success: function(r){
								var o = Ext.decode(r.responseText);
								if(o.success){
									GBIF.Notice.msg('Success', 'Deleted association successfully.');
									this.fireEvent('afterAssocDelete', this);
								} else {
									GBIF.Notice.msg('Error', o.error.msg);
								}
							}
						,	failure: function(result){
								GBIF.Notice.msg('Error','Error in connection.');
							} 
					}, this);
				}
			},this);
		}	
});